//
//  loginViewController.m
//  Travel
//
//  Created by lanou3g on 15/12/16.
//  Copyright © 2015年 涂晓雨. All rights reserved.
//

#import "loginViewController.h"
#import "RegistViewController.h"
#import "UserModel.h"
#import "DataBaseTool.h"
@interface loginViewController ()
@property(nonatomic,strong)NSMutableArray *users;
@property(nonatomic,strong)UITextField *user;
@property(nonatomic,strong)UITextField *pass;

@end

@implementation loginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"登陆.png"]];
    
    self.user = [[UITextField alloc]initWithFrame:CGRectMake(30, 80, self.view.frame.size.width - 60, 30)];
    self.user.placeholder = @"请输入用户名";
    self.user.borderStyle = UITextBorderStyleRoundedRect;
    
    [self.view addSubview:self.user];
    
    
    self.pass = [[UITextField alloc]initWithFrame:CGRectMake(30, 130, self.view.frame.size.width - 60, 30)];
    self.pass.placeholder = @"请输入密码";
    self.pass.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.pass];
    

    [self loginButton:CGRectMake(30, 180, ((self.view.frame.size.width - 60)/2) - 20, 30) title:@"登录" action:@selector(login)];
   
    [self loginButton:CGRectMake(((self.view.frame.size.width - 60)/2) + 30, 180, ((self.view.frame.size.width - 60)/2) - 20, 30) title:@"注册" action:@selector(Regist)];
    
   }



-(void)login{

    UserModel *model = [[UserModel alloc]init];
    model.name = self.user.text;
    model.password = self.pass.text;
    if (self.user.text.length > 0 && self.pass.text.length > 0) {
 
       if ([[DataBaseTool shareDataBaseTool]searchUserData:model]) {
           [self.delegate userName:self.user.text];
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
        
    }  
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或者密码输入错误" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alter animated:YES completion:nil];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alter addAction:action];

}


-(void)Regist{
    RegistViewController *regist = [[RegistViewController alloc]init];
    [self presentViewController:regist animated:YES completion:nil];

}

//登录按钮
-(void)loginButton:(CGRect)frame  title:(NSString *)str action:(SEL)action{
    UIButton *button = [[UIButton alloc]initWithFrame:frame];
    
    button.layer.masksToBounds = YES;
    
    button.layer.cornerRadius = 10;
    
    button.backgroundColor = [UIColor brownColor];
    
    [button setTitle:str forState:UIControlStateNormal];
    
    [self.view addSubview:button];
    
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
}
@end
